package org.lip6.struts.domain;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DAOContact {
private final static String RESOURCE_JDBC = "java:comp/env/jdbc/dsMyDB";
	
	/**
	 * Retourne la liste des courses stock�es dans la base.
	 * @return La liste des courses/erreurs.
	 */
	public DTOListContact displayContacts() {
		final DTOListContact returnedContact = new DTOListContact();
		
		try {
			//Context from Javax naming
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);

			final List<Contact> contactList = new LinkedList<Contact>();
			
			final Connection lConnection  = lDataSource.getConnection();
			final Statement lStatement = lConnection.createStatement();
			final ResultSet lResultSet = lStatement.executeQuery("SELECT * FROM CONTACT");
			
			while (lResultSet.next()) {
				final Contact lContact = new Contact();
				
				final Long id = lResultSet.getLong("ID_CONTACT");
				final String firstName = lResultSet.getString("FIRSTNAME");
				final String lastName = lResultSet.getString("LASTNAME");
				final String email = lResultSet.getString("EMAIL");
				
				lContact.setId(id);
				lContact.setFirstName(firstName);
				lContact.setLastName(lastName);
				lContact.setEmail(email);
				
				contactList.add(lContact);
			}
			
			returnedContact.setContactList(contactList);
			
		} catch (NamingException e) {
			// Trace errors in naming (the datasource resource in this case)
			e.printStackTrace();
			// save the error in the DTO
			returnedContact.setError("NamingException : " + e.getMessage());
		} catch (SQLException e) {
			// Trace SQL exceptions (bad columns name, JDBC errors, SQL errors, etc.
			e.printStackTrace();
			// save the error in the DTO
			returnedContact.setError("SQLException : " + e.getMessage());
		}
		
		return returnedContact;
	}

	public String addContact(final long id, final String firstName, final String lastName, final String email) {
		try {
			final Context lContext = new InitialContext();
			final DataSource lDataSource = (DataSource) lContext.lookup(RESOURCE_JDBC);
			final Connection lConnection  = lDataSource.getConnection();
			
			// adding a new contact
			final PreparedStatement lPreparedStatementCreation = 
					
			lConnection.prepareStatement("INSERT INTO CONTACT(ID_CONTACT, FIRSTNAME, LASTNAME, EMAIL) VALUES(?, ?, ?, ?)");
			
			lPreparedStatementCreation.setLong(1, id);
			lPreparedStatementCreation.setString(2, firstName);
			lPreparedStatementCreation.setString(3, lastName);
			lPreparedStatementCreation.setString(4, email);
			lPreparedStatementCreation.executeUpdate();
			
			return null;
		} catch (NamingException e) {
			e.printStackTrace();
		
			return "NamingException : " + e.getMessage();
		
		} catch (SQLException e) {
			e.printStackTrace();

			return "SQLException : " + e.getMessage();
			
		}
	}
	
	
}
